package org.gap.jseed.apf;

import java.lang.annotation.Annotation;
import java.util.Collection;
import java.util.Set;

import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.type.DeclaredType;

import org.gap.jseed.contract.annotation.NotEmpty;

public class NotEmptyProcessor implements AnnotatedMethodProcessor {

	@Override
	public Class<? extends Annotation> getSupportedAnnotation() {
		return NotEmpty.class;
	}

	@Override
	public void process(Set<? extends Element> elements, MessageWriter writer) {
		for (Element each : elements) {
			if (each instanceof ExecutableElement) {
				ExecutableElement exec = (ExecutableElement) each;
				if (exec.getParameters().isEmpty()) {
					writer.warningMessage(Messages.NotEmpty_NoParameters, exec);
					continue;
				}
				boolean hasCollections = false;
				for (Element element : exec.getParameters()) {
					if (element.asType() instanceof DeclaredType) {
						DeclaredType type = ((DeclaredType) element.asType());
						try {
							Class<?> instanceClass = Class.forName(type.toString().split("<")[0]);
							if (Collection.class.isAssignableFrom(instanceClass)) {
								hasCollections = true;
							}
						} catch (ClassNotFoundException e) {
							e.printStackTrace();
						}
					}
				}
				if (!hasCollections) {
					writer.warningMessage(Messages.NotEmpty_NoInstanceofCollection, exec);
				}
			}
		}
	}

}
